// pages/member-info/member-info.js
const common = require('../../utils/common.js');
const api = require('../../api/api.js');
const user_id = wx.getStorageSync('user_id');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['女','男','未知'],
    index:'',
    time:'选择生日',
    region: ['请选择地区'],
    mendian_arr:[],
    chose_mendian:-1,
    userInfo:{},
    user_name:'',
    zhiye_arr:['企业高管','私营业主','高级白领','高收入自由职业者','全职妈妈','中老年休闲一族'],
    chose_zhiye:''
  },

  //获取用户信息
  get_userInfo(){
    let that = this;
    common.request(api.get_user_info,{},'post').then((res) =>{
      if(res.status == 1){
        console.log(res)
        console.log(res.userInfo.user_sex)
        that.setData({
          userInfo:res.userInfo,
          index:res.userInfo.user_sex,
          time:res.userInfo.user_birthday,
          region:res.userInfo.user_addr||['请选择地区'],
          mandian:res.userInfo.shop_name,
          user_name:res.userInfo.user_name,
          user_nickname:res.userInfo.user_nickname,
          user_zhiye:res.userInfo.user_zhiye,
          mendianId:res.userInfo.shop_id,
        })
        if(res.userInfo.user_zhiye){
            const arr = that.data.zhiye_arr;
            const getIndex = ((arr,item)=>{
            for(var i in arr){
            if(arr[i] == item){
            return i;
            };
            };
            });
            that.setData({
              chose_zhiye:getIndex(arr,res.userInfo.user_zhiye)
            })
        }


      }else{
        wx.showToast({
          title: res.msg,
          icon:'none'
        })
      }
    })
   
  },
  
  //获取门店列表
  get_mendian(){
    let that = this;
    common.request(api.get_userShop,{},'post').then((res) =>{
      if(res.status == 1){
        console.log(res)
        that.setData({
          mendian_arr:res.shoplist,
        })
        console.log(that.data.mendian_arr)
      }
    })
  },

  bindZhiyeChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      chose_zhiye: e.detail.value
    })
  },
  //修改用户信息
  Edit_userInfo(){
    let that = this;
    var user_addr=that.data.region[0];
    
    console.log(data)
    if(that.data.index==2){
      common.showSuccessToast('请选择性别')
      return
    }
    // if(that.data.user_name==''){
    //   common.showSuccessToast('请填写真实姓名')
    //   return
    // }
    if(that.data.time==''){
      common.showSuccessToast('请选择生日')
      return
    }
    // if(user_addr=='请选择地区'){
    //   common.showSuccessToast('请选择地区')
    //   return
    // }
    if(!that.data.mendianId){
      common.showSuccessToast('请选择门店')
      return
    }
    // if(that.data.chose_zhiye==''){
    //   common.showSuccessToast('请选择职业')
    //   return
    // }
    let data = {
    //   user_name:that.data.user_name,
      user_sex : that.data.index,
    //   user_addr:that.data.region[0]+','+that.data.region[1]+','+that.data.region[2],
      user_birthday:that.data.time,
      shop_id:that.data.mendianId || 0,
    //   user_zhiye:that.data.zhiye_arr[that.data.chose_zhiye]
    }
    common.request(api.Edit_userInfo,data,'post').then((res) =>{
      if(res.status == 1){
        console.log(res)
        wx.showToast({
          title: '保存成功',
          icon:'none'
        })
        // that.get_userInfo()
        setTimeout(function () {
          wx.switchTab({
            url: '/pages/member/member',
          })
        }, 1000)
      }else{
        common.showSuccessToast(res.msg)
      }
    })
  },
  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  //选择生日
  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      time: e.detail.value
    })
  },

  //选择地区
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  bindMendianChange(e){
    this.setData({
      chose_mendian: e.detail.value,
      mendianId:this.data.mendian_arr[e.detail.value].id
    })
  },
  // 修改姓名
  // editName(){
  //   wx.navigateTo({
  //     url: '/pages/editName/editName',
  //   })
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.get_userInfo();
     this.get_mendian();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
     var pages = getCurrentPages();
    var currPage = pages[pages.length - 1]; //当前页面
    let json = currPage.data.user_name;
    this.setData({
      user_name:json
    })
    console.log(json) //为传过来的值
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})